﻿using System;
using System.Collections.Generic;
using SinaWeiboPlugin.Properties;
using Slippor.Douban.Radio.Assist;

namespace SinaWeiboPlugin
{
    public static class FortuneTeller
    {
        private static DateTime _lastFortuneDate;
        public static DateTime LastFortuneDate
        {
            get { return FortuneTeller._lastFortuneDate; }
            set { FortuneTeller._lastFortuneDate = value; }
        }

        public static bool FortunedToday
        {
            get { return _lastFortuneDate.Date == DateTime.Today; }
        }

        private static FortuneCollection _availabelFortunes;
        public static FortuneCollection AvailabelFortunes
        {
            get
            {
                if (_availabelFortunes == null)
                {
                    RefreshAvailabelFortunes(_settings.AvailableFortunes);
                }
                return _availabelFortunes;
            }
        }

        public static void RefreshAvailabelFortunes(String fortunes)
        {
            _availabelFortunes = new FortuneCollection(fortunes);
        }

        public static Fortune GetFortune()
        {
            bool random = _settings.RandomOrder;
            return AvailabelFortunes.GetFortune(random);
        }

        private static Settings _settings = SinaWeiboConfig.SettingManager.Settings as Settings;
        private static List<String> _fortunes;
        public static List<String> Fortunes
        {
            get
            {
                if (_fortunes == null)
                {
                    _fortunes = new List<String>();
                    String fortunes = _settings.Fortunes;
                    String[] fortunesList = fortunes.Split(',');
                    foreach (var item in fortunesList)
                    {
                        _fortunes.Add(item);
                    }
                }
                return _fortunes;
            }
        }

        public static void InitializeFortunes()
        {
            String[] _questions = { "今天会遇到的人", "今天会碰到的事情", "今天的爱情感觉", "今天的工作状态", "今天的学习状况", "今天心情会如何", "今天的运气如何", "今天的幸运歌曲", "昨天对今天产生的影响", "要小心的事情", "下一步该做的事情" };
            _settings.Fortunes = String.Join(",", _questions);
            _settings.AvailableFortunes = _settings.Fortunes;
            _settings.Save();
        }

        public static String BuildTweet(Fortune fortune)
        {
            if (_settings.FortuneWithArtists && fortune.Answer.Album != null)
            {
                return String.Format("{0} : {1}({2}) #电台占卜#", fortune.Question, fortune.Answer.Title, fortune.Answer.Album.ArtistString);
            }
            else
            {
                return String.Format("{0} : {1} #电台占卜#", fortune.Question, fortune.Answer.Title);
            }
        }
    }

    public class FortuneCollection : List<Fortune>
    {
        public bool Contains(String fortune)
        {
            foreach (var item in this)
            {
                if (item.Question == fortune)
                {
                    return true;
                }
            }
            return false;
        }

        public FortuneCollection(String fortunes)
        {
            String[] fortunesList = fortunes.Split(',');
            foreach (var item in fortunesList)
            {
                if (FortuneTeller.Fortunes.Contains(item))
                {
                    Fortune fortune = new Fortune()
                    {
                        Question = item,
                        Order = FortuneTeller.Fortunes.IndexOf(item),
                        InUse = false
                    };
                    Add(fortune);
                }
            }
        }

        public Fortune GetFortune(bool random)
        {
            List<Fortune> fortunes = new List<Fortune>();
            foreach (var item in this)
            {
                if (!item.InUse)
                {
                    fortunes.Add(item);
                }
            }
            fortunes.Sort();
            if (fortunes.Count > 0)
            {
                Fortune fortune = fortunes[0];
                if (random)
                {
                    Random rand = new Random();
                    int i = rand.Next(0, 100) % fortunes.Count;
                    fortune = fortunes[i];
                }
                fortune.InUse = true;
                return fortune;
            }
            else
            {
                return null;
            }
        }
    }

    public class Fortune : IComparable
    {
        public int Order
        {
            get;
            set;
        }

        public String Question
        {
            get;
            set;
        }

        public bool InUse
        {
            get;
            set;
        }

        public int CompareTo(object obj)
        {
            Fortune fortune = obj as Fortune;
            if (fortune != null)
            {
                return this.Order.CompareTo(fortune.Order);
            }
            else
            {
                throw new ArgumentNullException("参数为空");
            }
        }

        public Song Answer
        {
            get;
            set;
        }
    }
}
